using Game.Core;
using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class AmbulanceItem : BaseItem
	{
		private int type;

		private int curPointIndex;

		private float[] moveDists;

		private bool isPlayAnim;

		private float animTimer;

		private float startMoveX;

		private float targetMoveX;

		private float originalX;

		private float originalY;

		private float waitChangeSceneTimer;

		private int changeSceneId;

		private AmbulanceContainerItem ambulance;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			string[] array = data.itemData.scriptParams.Split(',');
			type = int.Parse(array[0]);
			if (type == 1)
			{
				moveDists = new float[1];
				moveDists[0] = float.Parse(array[1]);
				return;
			}
			changeSceneId = int.Parse(array[1]);
			moveDists = new float[2];
			moveDists[0] = float.Parse(array[2]);
			moveDists[1] = float.Parse(array[3]);
		}

		public override void ResetState()
		{
			base.ResetState();
			originalX = base.transform.position.x;
			originalY = base.transform.position.y;
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isPlayAnim)
			{
				animTimer += Time.deltaTime / 5f;
				base.transform.SetX(Mathf.Lerp(startMoveX, targetMoveX, animTimer));
				if (animTimer >= 1f)
				{
					if (ambulance != null)
					{
						ambulance.IsMove(false);
					}
					isPlayAnim = false;
					if (curPointIndex <= 1)
					{
						SetChildsEnabled(true);
					}
				}
			}
			if (waitChangeSceneTimer > 0f)
			{
				waitChangeSceneTimer -= Time.deltaTime / 2f;
				if (waitChangeSceneTimer <= 0f)
				{
					GlobalEventSystem.Fire(new BaseEvent("game.change_scene_by_ambulance", changeSceneId));
				}
			}
		}

		protected override void SetChildItemLayer(BaseItem item, int layer, int mapId)
		{
			base.SetChildItemLayer(item, layer, 102);
		}

		public void MoveToPoint0()
		{
			if (isPlayAnim || type != 1 || isPlayAnim)
			{
				return;
			}
			base.transform.SetX(base.transform.position.x + moveDists[0]);
			startMoveX = base.transform.position.x;
			targetMoveX = startMoveX - moveDists[0];
			animTimer = 0f;
			isPlayAnim = true;
			SetChildsEnabled(false);
			for (int i = 0; i < childItems.Count; i++)
			{
				BaseItem baseItem = childItems[i];
				if (baseItem != null && baseItem.itemData.scriptType == 154)
				{
					ambulance = (AmbulanceContainerItem)baseItem;
					ambulance.SetState(AmbulanceContainerItem.State.Close);
					ambulance.IsMove(true);
					break;
				}
			}
		}

		public void MoveToPoint1()
		{
			if (isPlayAnim || type != 2 || curPointIndex != 0)
			{
				return;
			}
			startMoveX = base.transform.position.x;
			targetMoveX = startMoveX - moveDists[0];
			curPointIndex = 1;
			animTimer = 0f;
			isPlayAnim = true;
			SetChildsEnabled(false);
			for (int i = 0; i < childItems.Count; i++)
			{
				BaseItem baseItem = childItems[i];
				if (baseItem != null && baseItem.itemData.scriptType == 154)
				{
					ambulance = (AmbulanceContainerItem)baseItem;
					ambulance.SetState(AmbulanceContainerItem.State.Close);
					ambulance.IsMove(true);
					break;
				}
			}
		}

		public void MoveToPoint2()
		{
			if (!isPlayAnim && type == 2 && curPointIndex == 1)
			{
				startMoveX = base.transform.position.x;
				targetMoveX = startMoveX - moveDists[1];
				curPointIndex = 2;
				animTimer = 0f;
				isPlayAnim = true;
				SetChildsEnabled(false);
				if (ambulance != null)
				{
					ambulance.IsMove(true);
				}
			}
		}

		public void DoorClosed()
		{
			if (!isPlayAnim && type == 2)
			{
				GlobalEventSystem.Fire(new BaseEvent("game.can_touch_ui", false));
				GlobalEventSystem.Fire(new BaseEvent("game.can_touch_scene", false));
				MoveToPoint2();
				if (changeSceneId > 0)
				{
					waitChangeSceneTimer = 1f;
				}
			}
		}

		private void SetChildsEnabled(bool val)
		{
			SetColliderEnabled(val);
			SetPlatformColliderEnable(val);
			for (int i = 0; i < childItems.Count; i++)
			{
				BaseItem baseItem = childItems[i];
				if (baseItem != null && baseItem.itemData.scriptType != 154)
				{
					baseItem.SetAllCollidersEnabled(val);
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["2"] = originalX.ToString("F5");
			obj["3"] = originalY.ToString("F5");
		}
	}
}
